package com.glorysoft.lipuxinpda.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.glorysoft.lipuxinpda.R;
import com.glorysoft.lipuxinpda.adapter.viewholder.BaseHolder;
import com.glorysoft.lipuxinpda.base.App;
import com.glorysoft.lipuxinpda.entity.KeyValueEntity;
import com.glorysoft.lipuxinpda.entity.MaterialDetailInfoEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MaterialExchangeAdapter extends RecyclerView.Adapter<MaterialExchangeAdapter.VerticalViewHolder> implements View.OnClickListener {

    public InnerItemOnclickListener mListener;
    private List<MaterialDetailInfoEntity> mList;

    public void setmList(List<MaterialDetailInfoEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }


    public void setOnInnerItemOnClickListener(InnerItemOnclickListener listener) {
        this.mListener = listener;
    }

    @Override
    public VerticalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new VerticalViewHolder(R.layout.item_recyclerview, parent, viewType);

    }

    @Override
    public void onBindViewHolder(VerticalViewHolder holder, int position) {
        holder.refreshData(mList, position);
        holder.btn_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClick(v, position);
            }
        });
        holder.btn_exchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClick(v, position);
            }
        });
        holder.tv_material_name.setText(mList.get(position).getMTM_TYPE_NAME());


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {

    }


    public static class VerticalViewHolder extends BaseHolder<List<MaterialDetailInfoEntity>>  {

        private RecyclerView rv_data_in;
        private TextView tv_material_name;
        private Button btn_exchange;
        private Button btn_down;

        private List<MaterialDetailInfoEntity> data;


        public VerticalViewHolder(int viewId, ViewGroup parent, int viewType) {
            super(viewId, parent, viewType);
            rv_data_in = itemView.findViewById(R.id.rv_data_in);
            tv_material_name = itemView.findViewById(R.id.tv_material_name);
            btn_exchange = itemView.findViewById(R.id.btn_exchange);
            btn_down = itemView.findViewById(R.id.btn_down);

//            btn_down.setOnClickListener(this);
//            btn_exchange.setOnClickListener(this);
        }

        @Override
        public void refreshData(List<MaterialDetailInfoEntity> data, int position) {
            super.refreshData(data, position);
            this.data = data;
//            item_recyclerview.setLayoutManager(new GridLayoutManager(this, ONE_LINE_SHOW_NUMBER, LinearLayoutManager.VERTICAL, false));
            rv_data_in.setLayoutManager(new LinearLayoutManager(App.Companion.getApp()));
            //设置个背景色
//            item_recyclerview.setBackgroundResource(R.color.colorPrimary);
            //设置Adapter
            VerticalAdapter verticalAdapter = new VerticalAdapter();
            verticalAdapter.setList(data.get(position).getLIST());
            rv_data_in.setAdapter(verticalAdapter);

        }

        private class VerticalAdapter extends RecyclerView.Adapter<ItemViewHolder> {

            private List<KeyValueEntity> list;

            public void setList(List<KeyValueEntity> list) {
                this.list = list;
                notifyDataSetChanged();
            }

            @NotNull
            @Override
            public ItemViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
                return new ItemViewHolder(R.layout.item_textview, parent, viewType);
            }

            @Override
            public void onBindViewHolder(ItemViewHolder holder, int position) {
                holder.refreshData(list, position);
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        }


    }

    /**
     * 通用子条目hodler
     */
    public static class ItemViewHolder extends BaseHolder<List<KeyValueEntity>> {

        private TextView tv_key;
        private TextView tv_value;

        public ItemViewHolder(int viewId, ViewGroup parent, int viewType) {
            super(viewId, parent, viewType);
            tv_key = itemView.findViewById(R.id.tv_key);
            tv_value = itemView.findViewById(R.id.tv_value);
//            ViewGroup.LayoutParams layoutParams = tv_key.getLayoutParams();
//            layoutParams.width = layoutParams.height = screenWidth / 3;
//            imageview_item.setLayoutParams(layoutParams);
        }

        @Override
        public void refreshData(List<KeyValueEntity> data, final int position) {

            tv_key.setText(data.get(position).getID_CH());
            tv_value.setText(data.get(position).getVALUE());

        }
    }

}
